package com.ruoyi.es.service;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.IndexRequest;
import co.elastic.clients.elasticsearch.core.IndexResponse;
import com.ruoyi.es.vo.EsReturnVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@Service
public class TestEsService implements ITestEsService {
    private final ElasticsearchClient client;

    @Autowired
    public TestEsService(ElasticsearchClient client) {
        this.client = client;
    }

    @Override
    public void testEs() {
        Map<String, Object> data = new HashMap<>();
        data.put("name", "虚假部门");
        data.put("id", 10001);

        IndexRequest<Map> request = new IndexRequest.Builder<Map>()
                .index("sysdept")
                .id("10001")
                .document(data)
                .build();

        IndexResponse response = null;
        try {
            response = client.index(request);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println(response.result().jsonValue());
    }

    @Override
    public EsReturnVo setIndex(Map map,String index) {
        String uuid = String.valueOf(new Date().getTime());
        IndexRequest<Map> request = new IndexRequest.Builder<Map>()
                .index(index)
                .id(uuid)
                .document(map)
                .build();
        IndexResponse response = null;
        try {
            response = client.index(request);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        EsReturnVo esReturnVo = new EsReturnVo();
        esReturnVo.setUuid(uuid);
        esReturnVo.setStatus(response.result().jsonValue());

        return esReturnVo;
    }




}
